﻿using System.Collections.Generic;
using System.Data;
using System.Data.SQLite;
using Infrastructure.Common.Data;
using TallyBook.DesignLib;

namespace TallyBook.DAL.Repository.Imp
{
    public class AccountRepository : IRepository<Account>
    {
        private readonly IDbHelper _dbHelper = DbFactory.Instance.CreateDbHelper();

        public Account Get(long id)
        {
            var entity = new Account
            {
                Id = id
            };
            string sqlStr = entity.EntityToSqlFromPrimaryKey();
            using (
                SQLiteDataReader sqlDataReader = _dbHelper.ExecuteReader(_dbHelper.GetConnection(), CommandType.Text,
                    sqlStr))
            {
                //判断是否可读，并且在使用数据前一定要运行才能读取
                if (!sqlDataReader.Read())
                    return null;
                entity.SqlToEntity(sqlDataReader);
                return entity;
            }
        }

        public int Insert(Account entity)
        {
            string sqlStr = entity.EntityToSqlFromInsert(entity);
            int query = _dbHelper.ExecuteNonQuery(_dbHelper.GetConnection(), CommandType.Text, sqlStr);
            return query;
        }

        public int Update(Account entity)
        {
            string sqlStr = entity.EntityToSqlFromUpdate(entity);
            int query = _dbHelper.ExecuteNonQuery(_dbHelper.GetConnection(), CommandType.Text, sqlStr);
            return query;
        }

        public int Delete(Account entity)
        {
            string sqlStr = entity.EntityToSqlFromDelete(entity);
            int query = _dbHelper.ExecuteNonQuery(_dbHelper.GetConnection(), CommandType.Text, sqlStr);
            return query;
        }

        public List<Account> GetAll()
        {
            var entity = new Account();
            string sqlStr = entity.EntityToSqlFromAll();
            using (DataSet dataset = _dbHelper.ExecuteDataset(_dbHelper.GetConnection(), CommandType.Text, sqlStr))
            {
                var list = new List<Account>();

                foreach (DataRow row in dataset.Tables[0].Rows)
                {
                    var newEntity = new Account();
                    newEntity.SqlToEntity(row);
                    list.Add(newEntity);
                }
                return list;
            }
        }

        public List<AccountType> GetName()
        {
            string sqlStr = SqlBuilder.GetAllComplex<AccountType>();
            using (DataSet dataset = _dbHelper.ExecuteDataset(_dbHelper.GetConnection(), CommandType.Text, sqlStr))
            {
                var list = new List<AccountType>();

                foreach (DataRow row in dataset.Tables[0].Rows)
                {
                    var newEntity = new AccountType();
                    newEntity.Id = (long) row[0];
                    newEntity.Name = (string) row[1];

                    list.Add(newEntity);
                }
                return list;
            }
        }
    }
}